package com.jymf.consume.controller;

import com.jymf.common.HttpJsonObject;
import com.jymf.consume.service.ClientService;
import com.jymf.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zyj
 * @create 2017-09-25
 **/
@Controller
@RequestMapping("/client")
public class ClientController extends HttpJsonObject {
    Logger log = LoggerFactory.getLogger(ClientController.class);
    @Autowired
    private ClientService clientService;

    @RequestMapping(value = "/infoList", method = RequestMethod.POST)
    public void getClientList(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("clientName", request.getParameter("clientName"));
        map.put("clientPhone", request.getParameter("clientPhone"));
        Page<JymfClientInfo> page = new Page<JymfClientInfo>();
        page.setPageNo( Integer.parseInt(request.getParameter("pageNumber")));
        page.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        page.setParams(map);
        List<JymfClientInfo> listArticle = clientService.getClientInfo(page);
        page.setRows(listArticle);
        this.outJson(page, response);
    }


    @RequestMapping(value = "/getClientInfo", method = RequestMethod.POST)
    public void getClientInfo(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> resultMap = new HashMap<String,Object>();
        boolean b = false;

        String clientid = request.getParameter("clientid");
        if(null!=clientid||!"".equals(clientid)){
            try {
                JymfClientInfo jymfClientInfo = clientService.getInfo(Integer.parseInt(clientid));
                resultMap.put("info", jymfClientInfo);
                b = true;
            }catch (Exception e){
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }

        resultMap.put("type", b);
        this.outJson(resultMap, response);
    }

    @RequestMapping(value = "/updateClientInfo", method = RequestMethod.POST)
    public void updateClientInfo(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        JymfClientInfo jymfClientInfo = new JymfClientInfo();
        String clientName = request.getParameter("clientName");
        boolean b = false;
        jymfClientInfo.setClientId(Integer.parseInt(request.getParameter("clientId")));
        jymfClientInfo.setClientName(clientName);
        jymfClientInfo.setClientPhone(request.getParameter("clientPhone"));
        jymfClientInfo.setRecordBook(request.getParameter("recordBook"));
        jymfClientInfo.setClientCode(request.getParameter("clientCode"));
        jymfClientInfo.setClientEmail(request.getParameter("clientEmail"));
        jymfClientInfo.setClientCard(request.getParameter("clientCard"));
        jymfClientInfo.setClientAddress(request.getParameter("clientAddress"));
        jymfClientInfo.setClientSex(request.getParameter("clientSex"));
        jymfClientInfo.setIsUse(request.getParameter("isUse"));
        jymfClientInfo.setClientRemark(request.getParameter("clientRemark"));

        if(clientName!=null&&!"".equals(clientName)){
            try {
                clientService.updateClientById(jymfClientInfo);
                b = true;
                resultMap.put("info", "修改成功");
            } catch (Exception e) {
                resultMap.put("info", "修改失败");
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }else {
            resultMap.put("info", "非法操作");
        }

        resultMap.put("type", b);
        this.outJson(resultMap, response);
    }

    //保存客户
    @RequestMapping(value = "/saveClientInfo", method = RequestMethod.POST)
    public void saveClientInfo(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        JymfClientInfo jymfClientInfo = new JymfClientInfo();
        String clientName = request.getParameter("clientName");
        boolean b = false;
        jymfClientInfo.setClientName(clientName);
        jymfClientInfo.setClientPhone(request.getParameter("clientPhone"));
        jymfClientInfo.setClientCode(request.getParameter("clientCode"));
        jymfClientInfo.setRecordBook(request.getParameter("recordBook"));
        jymfClientInfo.setClientEmail(request.getParameter("clientEmail"));
        jymfClientInfo.setClientCard(request.getParameter("clientCard"));
        jymfClientInfo.setClientAddress(request.getParameter("clientAddress"));
        jymfClientInfo.setClientSex(request.getParameter("clientSex"));
        jymfClientInfo.setIsUse(request.getParameter("isUse"));
        jymfClientInfo.setClientRemark(request.getParameter("clientRemark"));
        jymfClientInfo.setAddDate(new Date());

        if(clientName!=null&&!"".equals(clientName)){
            try {
                clientService.saveClientInfo(jymfClientInfo);
                b = true;
                resultMap.put("info", "保存成功");
            } catch (Exception e) {
                resultMap.put("info", "保存失败");
                e.printStackTrace();
                log.error(e.getMessage());
            }
        }else {
            resultMap.put("info", "非法操作");
        }
        resultMap.put("type", b);
        this.outJson(resultMap, response);
    }

    @RequestMapping(value = "/balanceList", method = RequestMethod.POST)
    public void getBalanceList(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("clientName", request.getParameter("clientName"));
        map.put("clientId", request.getParameter("clientId"));
        Page<Map<String, Object>> page = new Page<Map<String, Object>>();
        page.setPageNo( Integer.parseInt(request.getParameter("pageNumber")));
        page.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        page.setParams(map);
        List<Map<String, Object>> listArticle = clientService.getBalanceList(page);
        page.setRows(listArticle);
        this.outJson(page, response);
    }

    //充值
    @RequestMapping(value = "/clientCharge", method = RequestMethod.POST)
    public void clientCharge(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        boolean b = false;
        String clientId = request.getParameter("clientId");//客户id
        String chargeAccount = request.getParameter("chargeAccount");//金额
        String chargeType = request.getParameter("chargeType");//充值类型
        String remark = request.getParameter("remark");
        Users users = (Users) request.getSession().getAttribute("userInfo");

        if(users==null||users.getId()<0){
            resultMap.put("info", "登录账号失效，请重新登录");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }
        if(clientId==null||"".equals(clientId)||chargeType==null||"".equals(chargeType)){
            resultMap.put("info", "非法操作");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }

        if(chargeAccount==null||"".equals(chargeAccount)){
            resultMap.put("info", "请输入金额");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }



        try {
            //用户余额
            ClientBalance clientBalance = new ClientBalance();
            clientBalance.setClientinfoId(Integer.parseInt(clientId));
            clientBalance.setClientRecharge(new BigDecimal(chargeAccount));

            //充值明细
            ClientConsumeRecord clientConsumeRecord = new ClientConsumeRecord();
            clientConsumeRecord.setStaffId(users.getId());
            clientConsumeRecord.setClientId(Integer.parseInt(clientId));
            clientConsumeRecord.setRecordMoney(new BigDecimal(chargeAccount));
            clientConsumeRecord.setRecordTime(new Date());
            clientConsumeRecord.setRemark(remark);
            clientConsumeRecord.setRecordType(chargeType);
            clientConsumeRecord.setOperationType("CZ");

            resultMap = clientService.clientCharge(clientBalance, clientConsumeRecord, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultMap.put("type", false);
            resultMap.put("info", "充值失败");
        }

        this.outJson(resultMap, response);
    }

    //消费
    @RequestMapping(value = "/clientConsume", method = RequestMethod.POST)
    public void clientConsume(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> resultMap = new HashMap<String, Object>();
        boolean b = false;
        String clientId = request.getParameter("clientId");//客户id
        String consumeCount = request.getParameter("consumeCount");//金额
        String consumeType = request.getParameter("consumeType");//充值类型
        String remark = request.getParameter("remark");
        Users users = (Users) request.getSession().getAttribute("userInfo");

        if(users==null||users.getId()<0){
            resultMap.put("info", "登录账号失效，请重新登录");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }
        if(clientId==null||"".equals(clientId)||consumeType==null||"".equals(consumeType)){
            resultMap.put("info", "非法操作");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }

        if(consumeCount==null||"".equals(consumeCount)){
            resultMap.put("info", "请输入金额");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }

        //判断客户余额
        ClientBalance clientBalance1 = clientService.selectBalanceByClientId(Integer.parseInt(clientId));
        int compare = clientBalance1.getClientBalance().compareTo(new BigDecimal(consumeCount));
        if(compare<0){
            resultMap.put("info", "客户余额不足请先充值!");
            resultMap.put("type", b);
            this.outJson(resultMap, response);
            return;
        }

        try {
            //用户余额
            ClientBalance clientBalance = new ClientBalance();
            clientBalance.setClientinfoId(Integer.parseInt(clientId));
            clientBalance.setClientConsume(new BigDecimal(consumeCount));

            //充值明细
            ClientConsumeRecord clientConsumeRecord = new ClientConsumeRecord();
            clientConsumeRecord.setStaffId(users.getId());
            clientConsumeRecord.setClientId(Integer.parseInt(clientId));
            clientConsumeRecord.setRecordMoney(new BigDecimal(consumeCount));
            clientConsumeRecord.setRecordTime(new Date());
            clientConsumeRecord.setRemark(remark);
            clientConsumeRecord.setRecordType(consumeType);
            clientConsumeRecord.setOperationType("XF");

            resultMap = clientService.clientConsume(clientBalance, clientConsumeRecord, resultMap);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            resultMap.put("type", false);
            resultMap.put("info", "扣款失败");
        }

        this.outJson(resultMap, response);
    }

    @RequestMapping(value = "/getClientRecordList", method = RequestMethod.POST)
    public void getClientRecordList(HttpServletResponse response, HttpServletRequest request){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("clientName", request.getParameter("clientName"));
        map.put("clientId", request.getParameter("clientId"));
        map.put("operationType", request.getParameter("operationType"));
        map.put("startTime", request.getParameter("startTime"));
        map.put("endTime", request.getParameter("endTime"));
        Page<Map<String, Object>> page = new Page<Map<String, Object>>();
        page.setPageNo( Integer.parseInt(request.getParameter("pageNumber")));
        page.setPageSize(Integer.parseInt(request.getParameter("pageSize")));
        page.setParams(map);
        List<Map<String, Object>> listArticle = clientService.getClientRecordList(page);
        page.setRows(listArticle);
        this.outJson(page, response);
    }

}
